<?php
/**
 * Upload.php 文件上传
 *
 * @author 周剑锋 <camfee@foxmail.com>
 * @date   17-9-7 上午9:16
 *
 */

namespace Common;


class UploadOss
{
    /**
     * 保存图片
     *
     * @param string $filename 本地文件路径|http链接
     * @param string $uri      保存路径
     * @param int    $itemid   项目id
     * @return bool|string
     */
    public static function saveFile($filename, $uri, $itemid = 0)
    {
        $path = PhotoImage::getImageHash($itemid);
        if (strpos($filename, 'data:image/') === 0) {
            if (strpos($filename, 'data:image/png;base64') === 0) {
                $imageType = 'png';
            } elseif (strpos($filename, 'data:image/gif;base64') === 0) {
                $imageType = 'gif';
            } else {
                $imageType = 'jpg';
            }
        } else {
            $imageType = @PhotoImage::getImageType($filename);
            if (empty($imageType)) {
                $imageType = 'jpg';
            }
        }

        if (empty($path['hash3'])) {
            $md5 = substr(md5(__KEY__ . $itemid . ''), -5);
            $savePath = $uri . "{$path['hash1']}/{$path['hash2']}/{$itemid}_{$md5}.$imageType";
        } else {
            $md5 = substr(md5(__KEY__ . microtime() . ''), -5) . rand(100, 999);
            $savePath = $uri . "{$path['hash1']}{$path['hash2']}/{$path['hash3']}/{$md5}.$imageType";
        }

        if (stripos($filename, 'http') === 0) {
            $content = file_get_contents($filename);
            $res = Oss::SaveFile($savePath, $content, loadconf('oss/oss')['bucket']);
        } elseif (stripos($filename, 'data:image/') === 0) {
            $res = Oss::SaveFile($savePath, base64_decode(substr($filename, strpos($filename, ';base64,') + 8)),
                loadconf('oss/oss')['bucket']);
        } else {
            $res = Oss::PutFile($savePath, $filename, loadconf('oss/oss')['bucket']);
        }
        if (empty($res)) {
            debug_log("getActUrl failed,, {$filename}", JF_LOG_WARNING);

            return false;
        }

        return ResourcePathManager::getOssImageUrl() . $savePath;
    }


    /**
     * 保存文件
     *
     * @param array  $file   $file文件
     * @param string $uri    保存路径
     * @param int    $itemid 项目id
     * @return bool|string
     */
    public static function uploadFile($file, $uri, $itemid = 0)
    {
        $path = PhotoImage::getImageHash($itemid);
        $fileType = substr(strrchr($file['name'], '.'), 1);

        if (empty($path['hash3'])) {
            $md5 = substr(md5(__KEY__ . $itemid . ''), -5);
            $savePath = $uri . "{$path['hash1']}/{$path['hash2']}/{$itemid}_{$md5}.$fileType";
        } else {
            $md5 = substr(md5(__KEY__ . microtime() . ''), -5) . rand(100, 999);
            $savePath = $uri . "{$path['hash1']}{$path['hash2']}/{$path['hash3']}/{$md5}.$fileType";
        }

        $res = Oss::PutFile($savePath, $file['tmp_name'], loadconf('oss/oss')['bucket']);
        if (empty($res)) {
            debug_log("getActUrl failed,, {$file['tmp_name']}", JF_LOG_WARNING);

            return false;
        }

        return ResourcePathManager::getOssImageUrl() . $savePath;
    }


}